package com.lv.suyiyong.hfr.adapter

import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.Constants
import com.lv.common.widget.video.YGXVideoTopPlayer
import com.lv.service.bean.ViewTypeBean
import com.lv.suyiyong.hfr.R
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.util.BannerUtils


/**
 * 作者： ygx
 * 创建日期：2024/6/13 9:27 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：自定义布局，下面是常见的图片样式，更多实现可以看demo，可以自己随意发挥
 */
class BannerMediaNoBroderAdapter(
    mDatas: List<ViewTypeBean?>?,
    private val mLayout: Int = R.layout.item_video_banner_no_border,
    private val videoH: Float = 326.0f
) :
    BannerAdapter<ViewTypeBean, RecyclerView.ViewHolder?>(mDatas) {
    private val mVHMap = SparseArray<RecyclerView.ViewHolder>()
    private var gsyVideoOptionBuilder: GSYVideoOptionBuilder? = null

    private var mWidth: Int = 200

    //    private var height: Int = 132
    override fun getItemViewType(position: Int): Int {
//        var realPosition = (position - 1) % mDatas.size
//        if (realPosition < 0) {
//            realPosition = 0
//        }
        val realPosition = getRealPosition(position)
        when (mDatas[realPosition].type) {
            ViewTypeBean.PIC_TYPE -> return 0//图片
            ViewTypeBean.VEDIO_TYPE -> return 1//视频
            else -> return -1//未知
        }
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            0 -> {
                return BannerNoBorderMyViewHolder(
                    BannerUtils.getView(
                        parent,
                        R.layout.item_pic_banner_no_border
                    )
                )
            }
            1 -> {
                return BannerNoBorderMyViewVideoHolder(
                    BannerUtils.getView(
                        parent,
                        mLayout
                    )
                )
            }
            else -> {
                val text = TextView(parent.context)
                text.text = "未知"
                return BannerMyUndifindHolder(text)
            }
        }

    }

    override fun onBindView(
        holder: RecyclerView.ViewHolder?,
        data: ViewTypeBean?,
        position: Int,
        size: Int
    ) {
        val layoutPosition = holder?.layoutPosition ?: 0
        val itemViewType = holder?.itemViewType
        when (itemViewType) {
            ViewTypeBean.PIC_TYPE -> {
                val picHolder = holder as BannerNoBorderMyViewHolder
//                mVHMap.append(layoutPosition, picHolder)
                val requestOptions: RequestOptions = RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .priority(Priority.HIGH)
                    .format(DecodeFormat.PREFER_RGB_565)

                Glide.with(picHolder.imageView.context)
                    .setDefaultRequestOptions(requestOptions)
                    .load(data?.data?.imageFirst)
                    .override(SizeUtils.dp2px(360.0f), SizeUtils.dp2px(videoH))
                    .error(Constants.SYY_DEFAULT_GRAY)
                    .into(picHolder.imageView)
                picHolder.clParent.setOnClickListener {
                    onViewClickListener?.onClick(layoutPosition, data)
                }
            }
            ViewTypeBean.VEDIO_TYPE -> {
                val mVideoHolder = holder as BannerNoBorderMyViewVideoHolder
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
                mVHMap.append(layoutPosition, mVideoHolder)

                gsyVideoOptionBuilder = GSYVideoOptionBuilder()
                gsyVideoOptionBuilder?.setIsTouchWiget(true)?.setUrl(data?.data?.image)
                    ?.setStartAfterPrepared(true)
                    ?.setShowPauseCover(true)
                    ?.setCacheWithPlay(false)?.setRotateViewAuto(false)?.setLockLand(false)
                    ?.setPlayTag(layoutPosition.toString())
                    ?.setShowFullAnimation(true)?.setNeedLockFull(true)?.setLooping(true)
//                ?.setPlayPosition(lay)?.setMapHeadData(header)
                    ?.setVideoAllCallBack(object : GSYSampleCallBack() {
                        override fun onPrepared(url: String?, vararg objects: Any?) {
                            super.onPrepared(url, *objects)
                            if (!mVideoHolder.mVideo.isIfCurrentIsFullscreen) {
                                //静音
                                GSYVideoManager.instance().isNeedMute = true
                            }

                        }

                        override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
                            super.onQuitFullscreen(url, *objects)
                            //全屏不静音
                            GSYVideoManager.instance().isNeedMute = true
                        }

                        override fun onEnterFullscreen(url: String?, vararg objects: Any?) {
                            super.onEnterFullscreen(url, *objects)
                            GSYVideoManager.instance().isNeedMute = false
                        }
                    })?.build(mVideoHolder.mVideo)
                //隐藏title
                mVideoHolder.mVideo.titleTextView?.visibility = View.GONE
                //设置返回键
                mVideoHolder.mVideo.backButton?.visibility = View.GONE
                mVideoHolder.mVideo.fullscreenButton?.visibility = View.GONE
                mVideoHolder.mVideo.startPlayLogic()
                mVideoHolder.mVideo.setBottomShodowShow(false)
                mVideoHolder.mVideo.setShodowShow(false)
                mVideoHolder.mVideo.isAutoFullWithSize = true
                mVideoHolder.mVideo.isRotateWithSystem = true
                mVideoHolder.mVideo.isReleaseWhenLossAudio = true
//                mVideoHolder.mVideo.isLockLand = true

                mVideoHolder.mView.setOnClickListener {
                    onViewClickListener?.onClick(layoutPosition, data)
                }
            }
        }
    }

    class BannerNoBorderMyViewHolder(@NonNull view: View) :
        RecyclerView.ViewHolder(view) {
        var imageView: ImageView

        val clParent: ConstraintLayout

        init {
            imageView = view.findViewById(R.id.iv_pic)
            clParent = itemView.findViewById(R.id.cl_parent)

        }
    }

    class BannerNoBorderMyViewVideoHolder(@NonNull itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var mVideo = itemView.findViewById<YGXVideoTopPlayer>(R.id.ygx_video)
        val mView: View

        init {
            mView = itemView.findViewById(R.id.view)
        }
    }

    inner class BannerMyUndifindHolder(@NonNull view: View) :
        RecyclerView.ViewHolder(view) {

    }

    fun getVHMap(): SparseArray<RecyclerView.ViewHolder> {
        return mVHMap
    }

    fun getPlayManager(): GSYVideoOptionBuilder? {
        return gsyVideoOptionBuilder
    }

    var onViewClickListener: OnPositionTListener<ViewTypeBean?>? = null

}
